import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_plugin_gdmap/amap/amap_view.dart';
import 'package:flutter_plugin_gdmap/amap/amap_controller.dart';
import 'package:flutter_plugin_gdmap/amap/amap_options.dart';
import 'package:flutter_plugin_gdmap/flutter_plugin_gdmap.dart';
import 'package:flutter_plugin_gdmap/model/lat_lng.dart';

import 'bean/sort_marker_entity.dart';

class AMapPage extends StatefulWidget {
  @override
  _AMapPageState createState() => _AMapPageState();
}

class _AMapPageState extends State<AMapPage> {
  AmapLocation _amapLocation = AmapLocation();

  AMapController _controller;

  List<SortMarkerEntity> latLngs = <SortMarkerEntity>[];
  LatLng newLatLng = new LatLng(latitude: 30.68900214, longitude: 103.90422821);

  bool isget = false;

  String clickNum="--";

  @override
  void initState() {
    super.initState();
  }

  //实现监听
  Future<dynamic> platformCallHandler(MethodCall call) async {
    switch (call.method) {
      case "clickMarker":
        //点击了marker 排序列表改变状态
        int sortNum = int.parse(call.arguments);
        setState(() {
          clickNum=sortNum.toString();
        });
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        resizeToAvoidBottomInset: false,
        appBar: AppBar(
          title: Text('地图'),
        ),
        body: ConstrainedBox(
          constraints: BoxConstraints.expand(),
          child: Stack(
            alignment: Alignment.center, //指定未定位或部分定位widget的对齐方式
            children: <Widget>[
              AMapView(
                onAMapViewCreated: (controller) {
                  setState(() {
                    _controller = controller;
                    _controller.mapChannel.setMethodCallHandler(platformCallHandler); //设置监听
                  });
                  Future.delayed(Duration(seconds: 1), () {
                    changeList();
                  });
                },
                options: AmapOptions(showMyLocation: true),
              ),
              Positioned(
                top: 20,
                child: Column(
                  children: [
                    new ElevatedButton(
                        onPressed: changeList,
                        child: Text("切换")),
                    SizedBox(height: 10),
                    new ElevatedButton(
                        onPressed: look1,
                        child: Text("查看1")),
                    SizedBox(height: 10),
                    new ElevatedButton(
                        onPressed: look2,
                        child: Text("查看2")),
                    SizedBox(height: 10),
                    Text("地图点击了$clickNum",style: TextStyle(color: Colors.red,fontSize: 24),)
                  ],
                ),
              ),
            ],
          ),
        ));
  }

  Future<void> changeList() async {
    SortMarkerEntity entity = new SortMarkerEntity();
    entity.latitude = 30.68900214;
    entity.longitude = 103.90422821;
    entity.numSort = 1;
    entity.ticketNumber = 20;
    entity.phone = "13666666666";
    entity.address = "这是地址1这是地址1";

    SortMarkerEntity entity2 = new SortMarkerEntity();
    entity2.latitude = 30.60984528;
    entity2.longitude = 104.01580811;
    entity2.numSort = 2;
    entity2.ticketNumber = 30;
    entity2.phone = "1388888888";
    entity2.address = "这是地址2这是地址2";

    if (isget) {
      latLngs.add(entity);
      latLngs.add(entity2);
    } else {
      latLngs.add(entity2);
      latLngs.add(entity);
    }
    isget = !isget;

    Map<String, Object> map = {
      "location": json.encode(newLatLng),
      "list": json.encode(latLngs)
    };
    await _controller.changeList(map);
  }

  void look1() {
    //点移动到屏幕中心并显示地址等信息
    SortMarkerEntity entity = new SortMarkerEntity();
    entity.latitude = 30.68900214;
    entity.longitude = 103.90422821;
    entity.numSort = 1;
    entity.ticketNumber = 20;
    entity.phone = "13666666666";
    entity.address = "这是地址1这是地址1";

    Map<String, String> map = {
      "latLng": json.encode(entity),
      "list": json.encode(latLngs),
      "location": json.encode(newLatLng)
    };
    _controller.checkPoint(map);
    _setZoomTo(13);
  }

  void look2() {
    //点移动到屏幕中心并显示地址等信息
    SortMarkerEntity entity = new SortMarkerEntity();
    entity.latitude = 30.60984528;
    entity.longitude = 104.01580811;
    entity.numSort = 2;
    entity.ticketNumber = 30;
    entity.phone = "1388888888";
    entity.address = "这是地址2这是地址2";

    Map<String, String> map = {
      "latLng": json.encode(entity),
      "list": json.encode(latLngs),
      "location": json.encode(newLatLng)
    };
    _controller.checkPoint(map);
    _setZoomTo(13);
  }

  ///设置地图缩放比例
  void _setZoomTo(int num) {
    Map<String, dynamic> locationMap = {"num": num};
    Future.delayed(Duration(milliseconds: 500), () {
      //延时500毫秒执行。需要等待地图控件上个动画操作完成,地图操作以动画方式按照传入的CameraUpdate参数更新地图状态，默认动画耗时250毫秒。
      _controller.setZoomTo(locationMap);
    });
  }
}
